/**
 * 教研老师修改自己的科研题目信息
 * 
 * /jiaoyan/topic/update
 */



const Topics= require('../../../models/researchTopics')
const Topic_Students = require('../../../models/researchTopicStudents')
const MySQLManager = require('../../../utils/MySQLManager');
const sequelize = MySQLManager
const utils = require('../../../utils/index')
const $ = require('../../../utils/common')

const updata = async function (req, res) {        
    try {
        delete req.body.createdAt
        delete req.body.updatedAt
        // 年度不变， 报名人数不变， 确认人数不变
        delete req.body.year_id
        delete req.body.confirm_num
        delete req.body.current_student

        if (!req.body.id) {
            $.sendMessage(res, 1015, 'id不能缺少')
            return
        }

        // 只能修改自己的科研题目，先判断

        var topic_info = await Topics.findOne({
            where: {
                id: req.body.id,
                teacher_user_name: req.user_name
            }
        })

        // 防止水平越权
        if (topic_info == null) {
            $.sendMessage(res, 1015, '无效id！您只能编辑存在切属于您的课题')
            return
        }

        // 如果锁定，则清除所有报名且未被老师确认的学生
        // 直接置报名的学生为未报名，该课题的当前报名人数等于已确认人数
        if (req.body.topic_status == '1' || req.body.topic_status == '0') {
            // $.log('这是修改状态')
            if (req.body.topic_status == '1') {
                var update = await Topics.update(
                    {
                        topic_status: '1'
                    },
                    {
                        where: {
                            id: req.body.id
                        }
                    }
                   )

                res.json({
                    code: 200,
                    message: '解锁成功',
                    result: update
                })
                return
            }

            if (req.body.topic_status == '0') {
                $.log('锁定题目，报名学生人数等于确定学生人数')
                // 锁定题目
                var confirm_num = await Topics.findOne({
                    where: {
                        id: req.body.id
                    }
                })

                // transaction start
                return sequelize.transaction(function (t) {
                    return Topic_Students.update(
                    {
                        topic_id: '',
                        student_status: '1',
                        topic_name: '',
                        teacher_fullname: '',
                        teacher_id: ''
                    }
                    ,
                    {
                        where: {
                            topic_id: req.body.id,
                            student_status: '0'
                        }
                    }
                    , 
                    {transaction: t}).then(function (user) {
                    return Topics.update(
                        {
                            current_student: confirm_num.dataValues.confirm_num,
                            topic_status: '0'
                        },  
                        {
                            where: {
                                id: req.body.id
                            }
                        }
                        , {transaction: t});
                    })
                }).then(function (result) {
                    res.json({
                        code: 200,
                        message: '锁定成功！'
                    })
                    return
                }).catch(function (err) {
                    res.json({
                        code: 1017,
                        message: '锁定失败',
                        err: 'err' + err
                    })
                    return
                    // 事务已被回滚
                    // err 是拒绝 promise 链返回到事务回调的错误
                })
            }
            $.sendMessage(res, 0000, '联系管理员')
            return
        }

        else if (!req.body.topic_status) {
            // id 不变,防止修改id  
            // $.log('这是修改详情')
            var id =  req.body.id
            delete req.body.id
            
            var updata = await Topics.update(
                req.body,
                {
                    where: {
                        id: id
                    }
                }
            )

            if (updata[0] == '1') {
                res.json({
                    code: 200,
                    message: '编辑成功',
                })
                return
        }

        // 异常
        res.json({
            code: 0000,
            message: '代码错误，请联系管理员'
        })
            
        // 日志
        utils.create_a_log(req, '科研题目管理', '编辑')
        return
        }
    }
    catch(error) {
        res.json({
            message: "抓到异常",
            error: 'error' + error
        })
        utils.create_a_log(req, '科研题目管理', '编辑', '失败')
        return
    } 
}
    
module.exports = updata